import {message, Tag} from "antd";
import {ActionType, ProTable} from "@ant-design/pro-components";
import React, {useRef, useState} from "react";
import {ProFormInstance} from "@ant-design/pro-form/lib";
import {listQuestionSubmitByPage} from "@/services/ant-design-pro/questionSubmitController";

export const SubmitRecord = ({questionId}) => {

  const formRef = useRef<ProFormInstance>()
  const actionRef = useRef<ActionType>()
  const [pageRequest,setPageRequest] = useState<PageRequest>({
    current: 1,
    pageSize: 10
  })

  const columns = [
    {
      title: '状态',
      width: 100,
      dataIndex: 'message',
      valueType: 'textarea',
      hideInSearch: true,
      ellipsis: true,
      render:(text:string,record:API.QuestionSubmitVO) => {
        if (record?.status === 0){
          return <Tag color='green'>{text}</Tag>
        }else {
          return <Tag color='error'>{text}</Tag>
        }
      }
    },
    {
      title: '语言',
      width: 100,
      dataIndex: 'language',
      valueType: 'textarea',
      ellipsis: true,
      hideInSearch: true,
      render:(text:string) => {
        return <Tag color='success'>{text}</Tag>
      }
    },
    {
      title: '执行用时(ms)',
      width: 100,
      dataIndex: 'time',
      hideInSearch: true,
      render:(text:string) => {
        return text
      }
    },
    {
      title: '消耗内存(mb)',
      width: 120,
      dataIndex: 'memory',
      hideInSearch: true,
      render:(text:string) => {
        return text
      }
    },
    {
      title: '提交时间',
      dataIndex: 'createTime',
      valueType: 'date',
      hideInSearch: true,
    },
  ];

  /**
   * 代码提交记录
   */
  const querySubmitRecord = async (params:PageRequest) => {
    const res = await listQuestionSubmitByPage({
      questionId: questionId,
      ...params
    })
    setPageRequest({
      current:params?.current,
      pageSize:params?.pageSize
    })
    if (res?.code === 200){
      return {
        data: res?.data?.records,
        // success 请返回 true，
        // 不然 table 会停止解析数据，即使有数据
        success: res?.code === 200,
        // 不传会使用 data 的长度，如果是分页一定要传
        total: res?.data?.total,
      };
    }
    message?.error(res?.message)
  }
  return (
    <>
      <ProTable<API.InterfaceInfoEntity, any>
        // params 是需要自带的参数
        // 这个参数优先级更高，会覆盖查询表单的参数
        headerTitle={false}
        toolbar={{
          settings:undefined
        }}
        formRef={formRef}
        actionRef={actionRef}
        params={{}}
        columns={columns as any}
        pagination={pageRequest}
        rowKey="id"
        search={false}
        request={(params) => querySubmitRecord(params)}
      />
    </>

  )


}
